﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Service;
using S4Store.Models.ExhibitionKpi;

namespace S4Store.Controllers
{
    public class ExhibitionKpiController : ReportBaseController
    {
        public ExhibitionKpiService ExhibitionKpiService { get; set; }

        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            InjectReportModel(model);
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionKpiService);
                var data = ExhibitionKpiService.GetExhibitionWeekDataQuery().Where(x => x.StoreId == Store.Id).ToList();
                model.MonthDayData = data;

                return View(model);
            }
        }


        public ActionResult MonthTd(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionKpiService);
                var config = ExhibitionKpiService.GetExhibitionMonthConfig(y, m, Store.Id);                
                return View(config);
            }

        }


        public ActionResult InputExhibitionKpiMonthConfig(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionKpiService);
                var config = ExhibitionKpiService.GetExhibitionMonthConfig(y, m, Store.Id);
                if (config == null)
                {
                    config = new Domain.ExhibitionKpi.ExhibitionMonthConfig();
                    config.StoreId = Store.Id;
                    config.Month = new DateTime(y, m, 1);
                }
                return View(config);
            }
        }


        public JsonResult ModifyBackInputExhibitionMonthConfig(S4Store.Domain.ExhibitionKpi.ExhibitionMonthConfig config, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionKpiService);
                var dbconfig = ExhibitionKpiService.GetExhibitionMonthConfigQuery().FirstOrDefault(x => x.Id == config.Id);
                if (dbconfig == null)
                {
                    dbconfig = new Domain.ExhibitionKpi.ExhibitionMonthConfig();
                }
                if (isconfirm)
                {
                    dbconfig.IsConfirmed = true;
                }
                dbconfig.StoreId = Store.Id;
                dbconfig.Month = config.Month;
                dbconfig.XSMB = config.XSMB;
                dbconfig.DTGGTFJE = config.DTGGTFJE;
                dbconfig.NBSCLDJDS = config.NBSCLDJDS;
                dbconfig.SYYYZS = config.SYYYZS;
                dbconfig.GXYSYDFJS = config.GXYSYDFJS;
                dbconfig.GXJHTFS = config.GXJHTFS;
                dbconfig.CTGGJHTFS = config.CTGGJHTFS;

                ExhibitionKpiService.SubmitExhibitionMonthConfig(dbconfig);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }

        public ActionResult InputExhibitionKpiDayData(int y, int m, int d)
        {
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionKpiService);
                var data = ExhibitionKpiService.GetExhibitionWeekData(y, m, d, Store.Id);
                if (data == null)
                {
                    data = new Domain.ExhibitionKpi.ExhibitionWeekData();
                    data.StoreId = Store.Id;
                    data.Month = new DateTime(y, m, 1);
                    data.WeekIndex = d;
                }
                return View(data);
            }
        }



        public JsonResult ModifyBackInputExhibitionWeekData(S4Store.Domain.ExhibitionKpi.ExhibitionWeekData config, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionKpiService);
                config.StoreId = Store.Id;
                if (isconfirm)
                {
                    config.IsConfirmed = true;
                }

                var dbconfig = ExhibitionKpiService.GetExhibitionWeekDataQuery().FirstOrDefault(x => x.Id == config.Id);
                if (dbconfig == null)
                {
                    dbconfig = new Domain.ExhibitionKpi.ExhibitionWeekData();
                }
                if (isconfirm)
                {
                    dbconfig.IsConfirmed = true;
                }

                //dbconfig.StoreId = Store.Id;
                //dbconfig.Month = config.Month;
                //dbconfig.WeekIndex = config.WeekIndex;
                //dbconfig.GXZJTRE = config.GXZJTRE;
                //dbconfig.GXZT = config.GXZT;
                //dbconfig.ZTHDZT = config.ZTHDZT;
                //dbconfig.GXSJTFS = config.GXSJTFS;
                //dbconfig.CTGGZJTFE = config.CTGGZJTFE;
                //dbconfig.DTHDZT = config.DTHDZT;
                //dbconfig.CTGGSJTFS = config.CTGGSJTFS;
                //dbconfig.GGZLDHPC = config.GGZLDHPC;
                //dbconfig.JHSCQBSJCS = config.JHSCQBSJCS;
                //dbconfig.CSQBSJSJBGS = config.CSQBSJSJBGS;
                //dbconfig.JCS = config.JCS;
                //dbconfig.JCKHMTDYS = config.JCKHMTDYS;
                //dbconfig.JHZTHDS = config.JHZTHDS;
                //dbconfig.ZTHDSJ = config.ZTHDSJ;
                //dbconfig.ZTHDSJZXS = config.ZTHDSJZXS;
                //dbconfig.ZTHDTRZJ = config.ZTHDTRZJ;
                //dbconfig.ZTHDCHACGS = config.ZTHDCHACGS;
                //dbconfig.ZTHDXZJKS = config.ZTHDXZJKS;
                //dbconfig.ZTHDXGPGBGS = config.ZTHDXGPGBGS;
                //dbconfig.JHDTHDS = config.JHDTHDS;
                //dbconfig.DTHDZXSJ = config.DTHDZXSJ;
                //dbconfig.DTHDSJZXS = config.DTHDSJZXS;
                //dbconfig.DTHDTRZJ = config.DTHDTRZJ;
                //dbconfig.DTHDCHACGS = config.DTHDCHACGS;
                //dbconfig.DTHDMBYYS = config.DTHDMBYYS;
                //dbconfig.DTHDCGYYS = config.DTHDCGYYS;
                //dbconfig.DTHDZBKECXHS = config.DTHDZBKECXHS;
                //dbconfig.DTHDXGPGBGS = config.DTHDXGPGBGS;
                ExhibitionKpiService.SubmitExhibitionWeekData(config);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }

    }
}
